package com.zte.smbms_plus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zte.smbms_plus.pojo.SmbmsProvider;
import com.zte.smbms_plus.service.SmbmsProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/provider/")
public class SmbmsProviderController {

    @Autowired
    private SmbmsProviderService providerService;

    @RequestMapping("findPro/{current}")
    public String findPro(@PathVariable("current") Long current,SmbmsProvider provider,Model model){
        QueryWrapper qw=new QueryWrapper();
        if (provider.getProcode()!=null&&!"".equals(provider.getProcode())){
            qw.like("procode",provider.getProcode());
        }
        if (provider.getProname()!=null&&!"".equals(provider.getProname())){
            qw.like("proname",provider.getProname());
        }

        Page<SmbmsProvider> page=new Page<SmbmsProvider>(current,3);
        Page<SmbmsProvider> page1 = providerService.page(page,qw);
        model.addAttribute("pPage",page1);
        model.addAttribute("provider",provider);
        return "providerlist";
    }

    @RequestMapping("findProById")
    public String findProById(Long pid, Long pPc,Model model){
        SmbmsProvider provider = providerService.getById(pid);
        model.addAttribute("provider",provider);
        model.addAttribute("pPc",pPc);
        return "providerview";
    }

    @RequestMapping("goUpdatePro")
    public String goUpdatePro(Long pid,Long pPc,Model model){
        SmbmsProvider provider = providerService.getById(pid);
        model.addAttribute("provider",provider);
        model.addAttribute("pPc",pPc);
        return "providermodify";
    }
    @RequestMapping("doUpdatePro")
    public String doUpdatePro(SmbmsProvider provider,Long pPc,Model model){
        boolean flag = providerService.updateById(provider);
        if (flag){
            model.addAttribute("msg","修改成功！");
            return "forward:findPro/"+pPc;
        }else {
            model.addAttribute("msg","修改失败！");
            return "providermodify";
        }
    }

    @RequestMapping("delPro")
    public String delPro(Long pid,Long pPc,Model model){
        boolean flag = providerService.removeById(pid);
        if (flag){
            model.addAttribute("msg","删除成功！");
            return "forward:findPro/"+pPc;
        }else {
            model.addAttribute("msg","删除失败！");
            return "forward:findPro/"+pPc;
        }
    }

    @RequestMapping("proAdd")
    public String proAdd(SmbmsProvider provider,Model model){
        boolean flag = providerService.save(provider);
        if (flag){
            model.addAttribute("msg","添加成功！");
            return "forward:findPro/"+1;
        }else {
            model.addAttribute("msg","添加失败！");
            return "provideradd.html";
        }
    }

}
